package com.cysion.study.ui

import android.content.Intent
import android.os.Parcelable
import com.cysion.study.ktbox.base.BaseActivity
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.ktbox.utils.darkTextTheme
import com.cysion.other.color
import com.cysion.targetfun._subscribe
import com.cysion.study.uibox.bar.TopBar
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.R
import com.cysion.study.adapter.FriendAdapter
import com.cysion.study.helper.UserCache
import com.cysion.study.net.ApiClient
import com.cysion.study.widget.ShowToast
import kotlinx.android.synthetic.main.activity_friend.*
import java.util.*

//选择审批人员
class AppFriendActivity : BaseActivity() {

    private lateinit var proAdapter: FriendAdapter
    private val staffList: MutableList<String> = mutableListOf()
    private var type=""//必填 审批类型 0.请假 1.报销 2.调休

    override fun getLayoutId(): Int = R.layout.activity_friend

    override fun initView() {
        darkTextTheme(color(R.color.main_cl))
        type=intent.getBundleExtra("type").getString("type")

        topbar.apply {
            setTitle("选择联系人")
            setTexts("确认", TopBar.Pos.RIGHT)
            setOnTopBarClickListener { obj, pos ->
                if (pos == TopBar.Pos.LEFT) {
                    var sList: MutableList<String> = mutableListOf()
                    for (i in staffList) {
                        /*if (i.isCheck) {
                            sList.add(i)
                        }*/
                    }

                    var intent = Intent()
                    intent.putParcelableArrayListExtra("key", sList as ArrayList<out Parcelable>)
                    setResult(RESULT_OK, intent)
                    finish()
                }else if (pos == TopBar.Pos.RIGHT) {
                    var sList: MutableList<String> = mutableListOf()
                    for (i in staffList) {
                       /* if (i.isCheck) {
                            sList.add(i)
                        }*/
                    }

                    var intent = Intent()
                    intent.putParcelableArrayListExtra("key", sList as ArrayList<out Parcelable>)
                    setResult(RESULT_OK, intent)
                    finish()
                }

            }
        }
        initRecyclerView()
    }


    //加载数据
    override fun initData() {
        super.initData()
        //审批人员
        getDataList()//获取审批节点
    }

    private fun getDataList() {
        Alert.loading(self)
        ApiClient.api.approvalGetInfo(UserCache.token,type)
            .compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    if (it.size == 0) {
                        multiView.showEmpty()
                    } else {
                        for (i in it) {
//                            staffList.addAll(i.staffList)
                        }
                      /*  simpleAdaper.setDatas(staffList)
                        simpleAdaper.notifyDataSetChanged()*/
                        multiView.showContent()
                    }
                }
                _onError {
                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                }
            }
    }




    //初始化列表
    private fun initRecyclerView() {
        /*indexableLayout.setLayoutManager(LinearLayoutManager(this))
        simpleAdaper = AppentStaffContactAdapter(this,"")
        indexableLayout.setAdapter(simpleAdaper)
        simpleAdaper.setDatas(staffList)

        indexableLayout.setOverlayStyle_MaterialDesign(resources.getColor(R.color.colorAccent))
        // 全字母排序。  排序规则设置为：每个字母都会进行比较排序；速度较慢
        indexableLayout.setCompareMode(IndexableLayout.MODE_ALL_LETTERS)

        simpleAdaper.setOnItemClicks { adapterPosition, flag ->
            flag.isCheck = !flag.isCheck
            println("ac2222" + flag.stName+flag.isCheck)
            simpleAdaper.notifyDataSetChanged()
        }*/

    }


    override fun closeMvp() {
    }

}
